package org.etom.impl.connector.datatypes;

import java.util.Map;

import org.etom.connector.datatypes.IMoodleConnection;
import org.etom.connector.datatypes.IMoodleCourse;
import org.etom.connector.datatypes.IMoodleUser;
import org.etom.connector.service.ConnectorException;
import org.etom.impl.connector.util.XmlRpcClientWrapper;

/**
 * Implementation of the Moodle user.
 * The instances of that class are particularly mutable. 
 */
public class MoodleUserImpl implements IMoodleUser {
  
  boolean areRequestPropsSet;
  boolean isConnectionSet;
  private Long id;
  private XmlRpcClientWrapper xmlRpcClientWrapper;
//  private IMoodleCourse[] courses;
  private IMoodleConnection connection;
  private Map<String, String> requestProps;
  
  /**
   * Construct a new Moodle user.
   * 
   * @param id - the identifier of the user as is specified in the server
   * @param xmlRpcClientWrapper - xml rpc wrapper
   */
  public MoodleUserImpl(Long id, XmlRpcClientWrapper xmlRpcClientWrapper) {
    this.id = id;
    this.xmlRpcClientWrapper = xmlRpcClientWrapper;
    areRequestPropsSet = false;
    isConnectionSet = false;
  }

  /**
   * The method sets the request properties.
   * The properties are used in the connection to the server.
   * 
   * @param requestProps - the request properties
   */
  public void setRequestProperties(Map<String, String> requestProps) {
    if (requestProps == null) return;
    this.requestProps = requestProps;
    areRequestPropsSet = true;
  }
  
  /**
   * The method sets the connection.
   * The user will belongs to it.
   * 
   * @param connection - the parent connection
   */
  public void setConnection(IMoodleConnection connection) {
    if (connection == null) return;
    this.connection = connection;
    isConnectionSet = true;
  }
  
  /**
   * @see org.etom.connector.datatypes.IMoodleUser#getId()
   */
  public Long getId() {
    checkState();
    return id;
  }

  /**
   * @see org.etom.connector.datatypes.IMoodleUser#getCourses()
   * 
   * @throws IllegalStateException if the user is not fully initialized
   */
  public IMoodleCourse[] getCourses() throws ConnectorException {
    checkState();
//    if (courses == null) {
//      courses = xmlRpcClientWrapper.getMoodleCources(this, requestProps);
//    }
//    return courses;
    return xmlRpcClientWrapper.getMoodleCources(this, requestProps);
  }

  /**
   * @see org.etom.connector.datatypes.IMoodleUser#getConnection()
   */
  @Override
  public IMoodleConnection getConnection() {
    checkState();
    return connection;
  }
  
  private void checkState() {
    if (!areRequestPropsSet)
      throw new IllegalStateException(
          "The mandarory properties of the moodle user are not set!");
    if (!isConnectionSet)
      throw new IllegalStateException(
          "The mandarory connection of the moodle user is not set!");
  }
  
  @Override
  public boolean equals(Object other) {
    if (!(other instanceof IMoodleUser)) {
      return false;
    }
    IMoodleUser otherUser = (IMoodleUser) other;
    return this.id.equals(otherUser.getId());
  }

  @Override
  public int hashCode() {
    return this.id.hashCode();
  }
  
}
